<template>
  <el-dialog
    top="10vh"
    width="700px"
    title="添加/编辑"
    :visible.sync="dialogValue"
    :close-on-click-modal="false"
    append-to-body
  >
    <SubmitForm
      :rule-form="ruleForm"
      :rules="rules"
      :form-list="formList"
      @change="handleFormChange"
      @save="onSave"
      @close="dialogValue = false"
    />

  </el-dialog>
</template>

<script>
import { http } from '@/api'
const defaultForm = {
  name: '',
  type: 1,
  content: '',
  status: 1
}
export default {
  name: 'FormAdd',
  data() {
    return {
      dialogValue: false,
      ruleForm: {
        ...defaultForm
      },
      rules: {
        name: [{ required: true, message: '不能为空' }],
        type: [{ required: true, message: '不能为空' }],
      },
      formList: [
        {
          prop: 'name',
          name: 'input',
          label: '名称',
          colSpan: 24
        },
        {
          key: 'type',
          name: 'radio',
          label: '类型',
          options: [
            { name: '服务协议', value: 1 },
            { name: '隐私协议', value: 2 },
          ],
          colSpan: 12
        },
        {
          prop: 'content',
          name: 'rich',
          label: '协议内容',
          colSpan: 24
        },
        {
          prop: 'status',
          name: 'switch',
          label: '是否启用',
          activeValue: 1,
          inactiveValue: 0,
          colSpan: 12
        },
      ]
    }
  },
  methods: {
    handleFormChange({ key, val }) {

    },
    open(row) {
      this.dialogValue = true
      if (row) {
        http.policy.read(row.id).then(res => {
          this.ruleForm = {
            ...res.data
          }
        })
      } else {
        this.ruleForm = {
          ...defaultForm
        }
        console.log('this.ruleForm',this.ruleForm)
      }
    },
    onSave() {
      http.policy[this.ruleForm.id ? 'update' : 'create'](this.ruleForm.id, { ...this.ruleForm }).then(res => {
        this.$message.success('操作成功')
        this.dialogValue = false
        this.$emit('get-page')
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
